home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / util / Map.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  15.3 KB  |  372 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Map.java    1.27 98/09/30
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. /**
  18.  * An object that maps keys to values.  A map cannot contain duplicate keys;
  19.  * each key can map to at most one value.<p>
  20.  *
  21.  * This interface takes the place of the <tt>Dictionary</tt> class, which was
  22.  * a totally abstract class rather than an interface.<p>
  23.  *
  24.  * The <tt>Map</tt> interface provides three <i>collection views</i>, which
  25.  * allow a map's contents to be viewed as a set of keys, collection of values,
  26.  * or set of key-value mappings.  The <i>order</i> of a map is defined as
  27.  * the order in which the iterators on the map's collection views return their
  28.  * elements.  Some map implementations, like the <tt>TreeMap</tt> class, make
  29.  * specific guarantees as to their order; others, like the <tt>HashMap</tt>
  30.  * class, do not.<p>
  31.  *
  32.  * Note: great care must be exercised if mutable objects are used as map keys.
  33.  * The behavior of a map is not specified if the value of an object is changed
  34.  * in a manner that affects equals comparisons while the object is a
  35.  * key in the map.  A special case of this prohibition is that it is not
  36.  * permissible for a map to contain itself as a key.  While it is permissible
  37.  * for a map to contain itself as a value, extreme caution is advised: the
  38.  * equals and hashCode methods are no longer well defined on a such a map.<p>
  39.  *
  40.  * All general-purpose map implementation classes should provide two
  41.  * "standard" constructors: a void (no arguments) constructor which creates an
  42.  * empty map, and a constructor with a single argument of type <tt>Map</tt>,
  43.  * which creates a new map with the same key-value mappings as its argument.
  44.  * In effect, the latter constructor allows the user to copy any map,
  45.  * producing an equivalent map of the desired class.  There is no way to
  46.  * enforce this recommendation (as interfaces cannot contain constructors) but
  47.  * all of the general-purpose map implementations in the JDK comply.
  48.  *
  49.  * @author  Josh Bloch
  50.  * @version 1.27 09/30/98
  51.  * @see HashMap
  52.  * @see TreeMap
  53.  * @see Hashtable
  54.  * @see SortedMap
  55.  * @see Collection
  56.  * @see Set
  57.  * @since JDK1.2
  58.  */
  59. public interface Map {
  60.     // Query Operations
  61.  
  62.     /**
  63.      * Returns the number of key-value mappings in this map.  If the
  64.      * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  65.      * <tt>Integer.MAX_VALUE</tt>.
  66.      *
  67.      * @return the number of key-value mappings in this map.
  68.      */
  69.     int size();
  70.  
  71.     /**
  72.      * Returns <tt>true</tt> if this map contains no key-value mappings.
  73.      *
  74.      * @return <tt>true</tt> if this map contains no key-value mappings.
  75.      */
  76.     boolean isEmpty();
  77.  
  78.     /**
  79.      * Returns <tt>true</tt> if this map contains a mapping for the specified
  80.      * key.
  81.      *
  82.      * @param key key whose presence in this map is to be tested.
  83.      * @return <tt>true</tt> if this map contains a mapping for the specified
  84.      * key.
  85.      * 
  86.      * @throws ClassCastException if the key is of an inappropriate type for
  87.      *           this map.
  88.      * @throws NullPointerException if the key is <tt>null</tt> and this map
  89.      *            does not not permit <tt>null</tt> keys.
  90.      */
  91.     boolean containsKey(Object key);
  92.  
  93.     /**
  94.      * Returns <tt>true</tt> if this map maps one or more keys to the
  95.      * specified value.  More formally, returns <tt>true</tt> if and only if
  96.      * this map contains at least one mapping to a value <tt>v</tt> such that
  97.      * <tt>(value==null ? v==null : value.equals(v))</tt>.  This operation
  98.      * will probably require time linear in the map size for most
  99.      * implementations of the <tt>Map</tt> interface.
  100.      *
  101.      * @param value value whose presence in this map is to be tested.
  102.      * @return <tt>true</tt> if this map maps one or more keys to the
  103.      *         specified value.
  104.      */
  105.     boolean containsValue(Object value);
  106.  
  107.     /**
  108.      * Returns the value to which this map maps the specified key.  Returns
  109.      * <tt>null</tt> if the map contains no mapping for this key.  A return
  110.      * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
  111.      * map contains no mapping for the key; it's also possible that the map
  112.      * explicitly maps the key to <tt>null</tt>.  The <tt>containsKey</tt>
  113.      * operation may be used to distinguish these two cases.
  114.      *
  115.      * @param key key whose associated value is to be returned.
  116.      * @return the value to which this map maps the specified key, or
  117.      *           <tt>null</tt> if the map contains no mapping for this key.
  118.      * 
  119.      * @throws ClassCastException if the key is of an inappropriate type for
  120.      *           this map.
  121.      * @throws NullPointerException key is <tt>null</tt> and this map does not
  122.      *          not permit <tt>null</tt> keys.
  123.      * 
  124.      * @see #containsKey(Object)
  125.      */
  126.     Object get(Object key);
  127.  
  128.     // Modification Operations
  129.  
  130.     /**
  131.      * Associates the specified value with the specified key in this map
  132.      * (optional operation).  If the map previously contained a mapping for
  133.      * this key, the old value is replaced.
  134.      *
  135.      * @param key key with which the specified value is to be associated.
  136.      * @param value value to be associated with the specified key.
  137.      * @return previous value associated with specified key, or <tt>null</tt>
  138.      *           if there was no mapping for key.  A <tt>null</tt> return can
  139.      *           also indicate that the map previously associated <tt>null</tt>
  140.      *           with the specified key, if the implementation supports
  141.      *           <tt>null</tt> values.
  142.      * 
  143.      * @throws UnsupportedOperationException if the <tt>put</tt> operation is
  144.      *              not supported by this map.
  145.      * @throws ClassCastException if the class of the specified key or value
  146.      *               prevents it from being stored in this map.
  147.      * @throws IllegalArgumentException if some aspect of this key or value
  148.      *              prevents it from being stored in this map.
  149.      * @throws NullPointerException this map does not permit <tt>null</tt>
  150.      *            keys or values, and the specified key or value is
  151.      *            <tt>null</tt>.
  152.      */
  153.     Object put(Object key, Object value);
  154.  
  155.     /**
  156.      * Removes the mapping for this key from this map if present (optional
  157.      * operation).
  158.      *
  159.      * @param key key whose mapping is to be removed from the map.
  160.      * @return previous value associated with specified key, or <tt>null</tt>
  161.      *           if there was no mapping for key.  A <tt>null</tt> return can
  162.      *           also indicate that the map previously associated <tt>null</tt>
  163.      *           with the specified key, if the implementation supports
  164.      *           <tt>null</tt> values.
  165.      * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  166.      *         not supported by this map.
  167.      */
  168.     Object remove(Object key);
  169.  
  170.  
  171.     // Bulk Operations
  172.  
  173.     /**
  174.      * Copies all of the mappings from the specified map to this map
  175.      * (optional operation).  These mappings will replace any mappings that
  176.      * this map had for any of the keys currently in the specified map.
  177.      *
  178.      * @param t Mappings to be stored in this map.
  179.      * 
  180.      * @throws UnsupportedOperationException if the <tt>putAll</tt> method is
  181.      *           not supported by this map.
  182.      * 
  183.      * @throws ClassCastException if the class of a key or value in the
  184.      *               specified map prevents it from being stored in this map.
  185.      * 
  186.      * @throws IllegalArgumentException some aspect of a key or value in the
  187.      *              specified map prevents it from being stored in this map.
  188.      * 
  189.      * @throws NullPointerException this map does not permit <tt>null</tt>
  190.      *            keys or values, and the specified key or value is
  191.      *            <tt>null</tt>.
  192.      */
  193.     void putAll(Map t);
  194.  
  195.     /**
  196.      * Removes all mappings from this map (optional operation).
  197.      *
  198.      * @throws UnsupportedOperationException clear is not supported by this
  199.      *           map.
  200.      */
  201.     void clear();
  202.  
  203.  
  204.     // Views
  205.  
  206.     /**
  207.      * Returns a set view of the keys contained in this map.  The set is
  208.      * backed by the map, so changes to the map are reflected in the set, and
  209.      * vice-versa.  If the map is modified while an iteration over the set is
  210.      * in progress, the results of the iteration are undefined.  The set
  211.      * supports element removal, which removes the corresponding mapping from
  212.      * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
  213.      * <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> operations.
  214.      * It does not support the add or <tt>addAll</tt> operations.
  215.      *
  216.      * @return a set view of the keys contained in this map.
  217.      */
  218.     public Set keySet();
  219.  
  220.     /**
  221.      * Returns a collection view of the values contained in this map.  The
  222.      * collection is backed by the map, so changes to the map are reflected in
  223.      * the collection, and vice-versa.  If the map is modified while an
  224.      * iteration over the collection is in progress, the results of the
  225.      * iteration are undefined.  The collection supports element removal,
  226.      * which removes the corresponding mapping from the map, via the
  227.      * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
  228.      * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations.
  229.      * It does not support the add or <tt>addAll</tt> operations.
  230.      *
  231.      * @return a collection view of the values contained in this map.
  232.      */
  233.     public Collection values();
  234.  
  235.     /**
  236.      * Returns a set view of the mappings contained in this map.  Each element
  237.      * in the returned set is a <tt>Map.Entry</tt>.  The set is backed by the
  238.      * map, so changes to the map are reflected in the set, and vice-versa.
  239.      * If the map is modified while an iteration over the set is in progress,
  240.      * the results of the iteration are undefined.  The set supports element
  241.      * removal, which removes the corresponding mapping from the map, via the
  242.      * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
  243.      * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not support
  244.      * the <tt>add</tt> or <tt>addAll</tt> operations.
  245.      *
  246.      * @return a set view of the mappings contained in this map.
  247.      */
  248.     public Set entrySet();
  249.  
  250.     /**
  251.      * A map entry (key-value pair).  The <tt>Map.entrySet</tt> method returns
  252.      * a collection-view of the map, whose elements are of this class.  The
  253.      * <i>only</i> way to obtain a reference to a map entry is from the
  254.      * iterator of this collection-view.  These <tt>Map.Entry</tt> objects are
  255.      * valid <i>only</i> for the duration of the iteration; more formally,
  256.      * the behavior of a map entry is undefined if the backing map has been
  257.      * modified after the entry was returned by the iterator, except through
  258.      * the iterator's own <tt>remove</tt> operation, or through the
  259.      * <tt>setValue</tt> operation on a map entry returned by the iterator.
  260.      *
  261.      * @see Map#entrySet()
  262.      */
  263.     public interface Entry {
  264.         /**
  265.      * Returns the key corresponding to this entry.
  266.      *
  267.      * @return the key corresponding to this entry.
  268.      */
  269.     Object getKey();
  270.  
  271.         /**
  272.      * Returns the value corresponding to this entry.  If the mapping
  273.      * has been removed from the backing map (by the iterator's
  274.      * <tt>remove</tt> operation), the results of this call are undefined.
  275.      *
  276.      * @return the value corresponding to this entry.
  277.      */
  278.     Object getValue();
  279.  
  280.         /**
  281.      * Replaces the value corresponding to this entry with the specified
  282.      * value (optional operation).  (Writes through to the map.)  The
  283.      * behavior of this call is undefined if the mapping has already been
  284.      * removed from the map (by the iterator's <tt>remove</tt> operation).
  285.      *
  286.      * @param value new value to be stored in this entry.
  287.      * @return old value corresponding to the entry.
  288.          * 
  289.      * @throws UnsupportedOperationException if the <tt>put</tt> operation
  290.      *          is not supported by the backing map.
  291.      * @throws ClassCastException if the class of the specified value
  292.      *           prevents it from being stored in the backing map.
  293.      * @throws    IllegalArgumentException if some aspect of this value
  294.      *          prevents it from being stored in the backing map.
  295.      * @throws NullPointerException the backing map does not permit
  296.      *          <tt>null</tt> values, and the specified value is
  297.      *          <tt>null</tt>.
  298.          */
  299.     Object setValue(Object value);
  300.  
  301.     /**
  302.      * Compares the specified object with this entry for equality.
  303.      * Returns <tt>true</tt> if the given object is also a map entry and
  304.      * the two entries represent the same mapping.  More formally, two
  305.      * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
  306.      * if<pre>
  307.          *     (e1.getKey()==null ?
  308.          *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &&
  309.          *     (e1.getValue()==null ?
  310.          *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
  311.          * </pre>
  312.      * This ensures that the <tt>equals</tt> method works properly across
  313.      * different implementations of the <tt>Map.Entry</tt> interface.
  314.      *
  315.      * @param o object to be compared for equality with this map entry.
  316.      * @return <tt>true</tt> if the specified object is equal to this map
  317.      *         entry.
  318.          */
  319.     boolean equals(Object o);
  320.  
  321.     /**
  322.      * Returns the hash code value for this map entry.  The hash code
  323.      * of a map entry <tt>e</tt> is defined to be: <pre>
  324.      *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
  325.      *     (e.getValue()==null ? 0 : e.getValue().hashCode())
  326.          * </pre>
  327.      * This ensures that <tt>e1.equals(e2)</tt> implies that
  328.      * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
  329.      * <tt>e1</tt> and <tt>e2</tt>, as required by the general
  330.      * contract of <tt>Object.hashCode</tt>.
  331.      *
  332.      * @return the hash code value for this map entry.
  333.      * @see Object#hashCode()
  334.      * @see Object#equals(Object)
  335.      * @see #equals(Object)
  336.      */
  337.     int hashCode();
  338.     }
  339.  
  340.     // Comparison and hashing
  341.  
  342.     /**
  343.      * Compares the specified object with this map for equality.  Returns
  344.      * <tt>true</tt> if the given object is also a map and the two Maps
  345.      * represent the same mappings.  More formally, two maps <tt>t1</tt> and
  346.      * <tt>t2</tt> represent the same mappings if
  347.      * <tt>t1.entrySet().equals(t2.entrySet())</tt>.  This ensures that the
  348.      * <tt>equals</tt> method works properly across different implementations
  349.      * of the <tt>Map</tt> interface.
  350.      *
  351.      * @param o object to be compared for equality with this map.
  352.      * @return <tt>true</tt> if the specified object is equal to this map.
  353.      */
  354.     boolean equals(Object o);
  355.  
  356.     /**
  357.      * Returns the hash code value for this map.  The hash code of a map
  358.      * is defined to be the sum of the hashCodes of each entry in the map's
  359.      * entrySet view.  This ensures that <tt>t1.equals(t2)</tt> implies
  360.      * that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps
  361.      * <tt>t1</tt> and <tt>t2</tt>, as required by the general
  362.      * contract of Object.hashCode.
  363.      *
  364.      * @return the hash code value for this map.
  365.      * @see Map.Entry#hashCode()
  366.      * @see Object#hashCode()
  367.      * @see Object#equals(Object)
  368.      * @see #equals(Object)
  369.      */
  370.     int hashCode();
  371. }
  372.